

dataset = 'opp'

dsads = {
    "RSC": [80.51, 85.76, 71.75, 86.42, 80.52, 81.82, 83.35, 75.45, 80.69],
    "Transformer": [81.28, 81.81, 66.19, 86.09, 78.44, 86.13, 81.19, 75.81, 79.61],
    "GILE": [76.35, 80.98, 70.84, 70.80, 72.81, 83.52, 82.41, 83.17, 77.61],
    "Triple-Res": [87.15, 91.83, 74.55, 89.15, 90.39, 91.39, 89.56, 84.24, 87.28],
    "GRU-INC": [83.06, 84.89, 69.62, 86.54, 80.25, 86.31, 88.02, 77.86, 82.06],
    "ELK": [88.40, 92.45, 74.41, 91.89, 86.64, 89.79, 87.82, 82.04, 86.68],
    "MDD-VAE": [89.67, 89.33, 78.66, 91.98, 91.33, 91.92, 90.21, 86.03, 88.63]
}

pamap2 = {
    "RSC": [62.68, 72.03, 79.35, 83.25, 74.67, 75.2, 75.01, 63.27, 73.23],
    "Transformer": [62.49, 66.45, 69.57, 72.36, 73.13, 70.32, 77.73, 67.02, 69.88],
    "GILE": [65.81, 69.93, 77.56, 72.53, 66.36, 71.63, 75.24, 61.19, 70.03],
    "Triple-Res": [67.15, 70.33, 78.97, 79.76, 76.95, 78.04, 81.03, 70.66, 75.36],
    "GRU-INC": [62.35, 72.68, 79.23, 77.67, 75.91, 80.88, 82.42, 64.09, 74.42],
    "ELK": [62.91, 71.87, 77.01, 80.10, 77.07, 73.60, 79.77, 68.67, 73.87],
    "MDD-VAE": [68.03, 72.76, 79.51, 83.74, 77.14, 78.70, 81.62, 73.31, 76.85]
}

unimib = {
    "RSC": [80.21, 83.65, 81.31, 74.78, 79.98],
    "Transformer": [75.62, 82.78, 78.73, 69.64, 76.68],
    "GILE": [76.99, 76.92, 75.70, 72.42, 75.53],
    "Triple-Res": [84.84, 85.37, 85.93, 79.43, 84.43],
    "GRU-INC": [78.95, 81.92, 84.06, 76.99, 80.48],
    "ELK": [82.85, 80.16, 84.19, 79.95, 81.78],
    "MDD-VAE": [87.52, 88.08, 89.06, 86.92, 87.89]
}

opp = {
    "RSC": [75.56, 75.14, 73.42, 74.98, 74.77],
    "Transformer": [80.78, 75.02, 71.98, 76.24, 76.07],
    "GILE": [79.23, 76.01, 73.53, 76.83, 76.42],
    "Triple-Res": [83.11, 75.99, 76.06, 80.78, 78.98],
    "GRU-INC": [81.04, 74.82, 73.72, 78.69, 77.05],
    "ELK": [79.23, 76.01, 73.53, 76.83, 76.42],
    "MDD-VAE": [84.14, 80.15, 77.21, 81.75, 80.81]
}
# mhealth = {
#     "ConvLSTM": [76.41, 73.63, 73.86, 73.38, 76.28, 74.71],
#     "CNN": [81.44, 80.42, 76.14, 79.02, 84.52, 80.31],
#     "ResNet": [81.33, 80.43, 76.62, 79.16, 83.22, 80.15],
#     "DanHAR": [78.18, 77.83, 77.10, 80.47, 82.34, 79.18],
#     "Tri-Res": [82.50, 79.19, 76.93, 79.44, 84.11, 80.43],
#     "GRU-INC": [76.47, 76.64, 74.94, 77.26, 79.21, 76.90],
#     "MTSDNet": [81.01, 80.97, 76.18, 80.29, 84.88, 80.67],
#     "MA-CNN": [82.88, 82.84, 80.75, 82.67, 86.08, 83.04],
#     "MA-Res": [84.02, 82.89, 80.57, 82.51, 84.29, 82.86]
# }

# pamap2 = {
#     "ConvLSTM": [69.74, 71.09, 67.95, 68.76, 68.84, 64.61, 71.82, 70.60, 69.18],
#     "CNN": [68.36, 72.56, 78.97, 77.10, 77.54, 76.16, 77.30, 74.50, 75.31],
#     "ResNet": [67.59, 70.53, 78.78, 77.69, 76.87, 76.36, 78.77, 73.64, 75.03],
#     "DanHAR": [68.30, 76.15, 81.69, 79.98, 77.59, 73.06, 81.18, 75.35, 76.66],
#     "Tri-Res": [67.34, 73.49, 79.40, 80.21, 77.55, 79.34, 81.28, 71.76, 76.30],
#     "GRU-INC": [66.28, 69.04, 77.47, 79.75, 73.42, 70.34, 80.28, 74.13, 73.84],
#     "MTSDNet": [66.35, 75.76, 80.81, 83.26, 77.73, 77.21, 81.99, 72.41, 76.94],
#     "MA-CNN": [73.78, 76.91, 82.35, 76.52, 79.19, 76.05, 83.29, 78.47, 78.32],
#     "MA-Res": [73.75, 78.56, 83.80, 81.73, 78.47, 75.28, 80.24, 77.09, 78.62]
# }

# # ucihar
# ucihar = {
#     "ConvLSTM": [90.12, 86.04, 90.56, 97.58, 98.12, 95.93, 93.06],
#     "CNN": [92.13, 86.23, 93.35, 97.38, 98.83, 96.03, 93.99],
#     "ResNet": [92.94, 89.85, 93.49, 95.61, 96.78, 95.89, 94.09],
#     "DanHAR": [92.95, 91.45, 94.58, 97.77, 97.89, 97.16, 95.30],
#     "Tri-Res": [92.27, 90.95, 95.62, 97.61, 98.56, 96.33, 95.22],
#     "GRU-INC": [92.83, 83.40, 95.06, 96.81, 98.05, 94.03, 93.36],
#     "MTSDNet": [91.56, 89.71, 93.54, 95.91, 96.12, 97.05, 93.98],
#     "MA-CNN": [93.01, 89.93, 96.43, 97.98, 98.52, 97.34, 95.54],
#     "MA-Res": [93.17, 91.26, 95.52, 97.85, 99.11, 97.87, 95.80]
# }

# # opp

# opp = {
#     "ConvLSTM": [74.81, 71.78, 69.33, 73.42, 72.34],
#     "CNN": [81.24, 78.53, 76.63, 82.55, 79.74],
#     "ResNet": [82.89, 76.83, 76.10, 81.35, 79.29],
#     "DanHAR": [83.24, 77.48, 76.08, 81.37, 79.54],
#     "Tri-Res": [83.61, 81.04, 78.37, 81.89, 81.23],
#     "GRU-INC": [81.64, 76.38, 73.13, 78.67, 77.45],
#     "MTSDNet": [82.60, 80.04, 77.39, 80.92, 80.22],
#     "MA-CNN": [83.96, 81.16, 79.36, 82.88, 81.84],
#     "MA-Res": [85.69, 81.67, 80.79, 83.64, 82.95]
# }


import matplotlib.pyplot as plt
import numpy as np

# 设置全局字体为 Times New Roman
plt.rcParams['font.family'] = 'Times New Roman'
plt.rcParams['font.size'] = 12  # 设置默认字体大小

# 示例数据（替换为你的实际数据）

data = eval(dataset)

# 提取模型名称和平均值（每个列表的最后一个数）
models = list(data.keys())
avg_accuracy = [values[-1] for values in data.values()]  # 提取每个列表的最后一个数作为平均值

# 计算准确率的变化幅度（最大值 - 最小值），忽略最后一个数（平均值）
accuracy_min = [min(values[:-1]) for values in data.values()]  # 最小值（忽略最后一个数）
accuracy_max = [max(values[:-1]) for values in data.values()]  # 最大值（忽略最后一个数）
accuracy_range = [max(values[:-1]) - min(values[:-1]) for values in data.values()]  # 变化幅度

# 设置画布
plt.figure(figsize=(10, 6))

# 定义颜色列表（每个柱子一个颜色）
colors = plt.cm.tab10.colors  # 使用 matplotlib 的 tab10 颜色映射
if len(models) > len(colors):  # 如果模型数量多于颜色数量，循环使用颜色
    colors = colors * (len(models) // len(colors)) + colors[:len(models) % len(colors)]

# 绘制柱状图
x = np.arange(len(models))  # x轴位置
width = 0.6  # 柱状图宽度
bars = []
for i, model in enumerate(models):
    bar = plt.bar(x[i], avg_accuracy[i], width, color=colors[i], edgecolor='black', label=model)
    bars.append(bar)

# 添加误差条（表示变化幅度）
plt.errorbar(x, avg_accuracy, yerr=[avg_accuracy[i] - accuracy_min[i] for i in range(len(models))], 
             fmt='none', color='black', capsize=5, label='F1 Score Range')

# 添加数据标签（显示平均准确率）
for i, bar in enumerate(bars):
    height = bar[0].get_height()
    # 在柱子上方显示平均准确率
    plt.text(bar[0].get_x() + bar[0].get_width() / 2, height, f'{avg_accuracy[i]:.2f}', 
             ha='center', va='bottom', fontsize=11, fontfamily='Times New Roman')
    # 在柱子下方显示 baseline 名称
    plt.text(bar[0].get_x() + bar[0].get_width() / 2, 0, models[i], 
             ha='center', va='bottom', fontsize=10, fontfamily='Times New Roman', rotation=45)

# 添加标签、标题和图例
plt.xlabel('Compared Works', fontsize=14, fontfamily='Times New Roman')
plt.ylabel('F1 Score(%)', fontsize=14, fontfamily='Times New Roman')
plt.title(f'{dataset.upper()} Average F1 Score with Range', 
          fontsize=16, fontfamily='Times New Roman')
plt.xticks([])  # 隐藏 x 轴刻度标签（因为名称已经在柱子下方显示）
plt.legend(loc='upper left', bbox_to_anchor=(1, 1), prop={'family': 'Times New Roman', 'size': 12})  # 设置图例字体
plt.grid(axis='y', linestyle='--', alpha=0.7)

# 保存图表为 SVG 格式，DPI=600
output_path = f"chap04_meta_domain_distangle_{dataset}.svg"  # 保存路径
plt.savefig(output_path, format="svg", dpi=600, bbox_inches="tight")

# 关闭图表（避免在脚本中显示）
plt.close()